import {defineStore} from 'pinia'

import {EmptyUtil, TokenUtil} from '@/utils'
import {AccountApi} from '@/libs/api'
import {useTabStore ,useAclStore} from '@/libs/store'

// @ts-ignore
export const useOperatorStore = defineStore('operator', {
    state: () => ({
        // 身份令牌
        token: TokenUtil.getToken(),
        // 操作员资料
        info: {}
    }),
    getters: {
        getToken: state => state.token,
        hasLogin: state => EmptyUtil.isNotEmptyString(state.token),
        hasInfo: state => EmptyUtil.isNotEmptyObject(state.info),
        // @ts-ignore
        accountName: state => EmptyUtil.isNotEmptyObject(state.info) ? state.info ?.name :'未登录',
        // @ts-ignore
        accountAvatar: state =>  state.info?.avatar  ? state.info.avatar :'/resource/imgs/avatar.png',
    },
    actions: {
        // 本地Action
        setToken(token) {
            this.token = token
            TokenUtil.setToken(token)
        },

        async getInfo() {
            const {data} = await AccountApi.getSimpleInfo()
            this.info = data
        },

        async logout() {
            await AccountApi.logout()
            await this.removeAll()
        },

        removeAll() {

            useTabStore().removeAll()
            useAclStore().removeAll()
            TokenUtil.removeToken()
            this.info = {}
            this.token = ''
        },
    },
})
